#include "PacmanFrame.h"

PacmanFrame::PacmanFrame()
{
	m_eatGhost = false;
	wolfenstein3dMode=false;
}

PacmanFrame::~PacmanFrame()
{
}

void PacmanFrame::Init(int x, int y)
{	
	//Frame 1
	m_pPacMesh.LoadXFile("res/pac.x");
	m_PacFrame.AddObject(&m_pPacMesh);
	m_PacFrame.SetPosition(0,0.5f,0);
	m_PacFrame.SetCallback(FC_TogglePac);

	m_pManMesh.LoadXFile("res/man.x");
	m_ManFrame.AddObject(&m_pManMesh);
	m_ManFrame.SetPosition(0,0.5f,0);
	m_ManFrame.SetCallback(FC_ToggleMan);

	m_Frame1.AddFrame(&m_PacFrame);
	m_Frame1.AddFrame(&m_ManFrame);
	m_Frame1.SetPosition(0,0,0);

	//Frame 2
	m_pPacMesh2.LoadXFile("res/pac_boos.x");
	m_PacFrame2.AddObject(&m_pPacMesh2);
	m_PacFrame2.SetPosition(0,0.5f,0);
	m_PacFrame2.SetCallback(FC_TogglePac);

	m_pManMesh2.LoadXFile("res/man_boos.x");
	m_ManFrame2.AddObject(&m_pManMesh2);
	m_ManFrame2.SetPosition(0,0.5f,0);
	m_ManFrame2.SetCallback(FC_ToggleMan);

	m_Frame2.AddFrame(&m_PacFrame2);
	m_Frame2.AddFrame(&m_ManFrame2);
	m_Frame2.SetPosition(0,0,0);
		
	m_Frame2.SetCallback(FC_RotateStart);
	m_Frame1.SetCallback(FC_RotateStart);
}

void PacmanFrame::SetPosition(float x, float y, float z)
{
	m_Frame1.SetPosition(x,y,z);
	m_Frame2.SetPosition(x,y,z);
}
void PacmanFrame::Render()
{
	if(m_eatGhost == true)
		m_Frame2.Render();
	else
		m_Frame1.Render();	
}
void PacmanFrame::Update()
{
	if(m_eatGhost == true)
		m_Frame2.Update();
	else
		m_Frame1.Update();	
}

void PacmanFrame::Move(int direction)
{
	if(direction == 0)
	{
		m_Frame1.SetCallback(FC_StopMovePac);
		m_Frame2.SetCallback(FC_StopMovePac);
	}
}
void PacmanFrame::Turn(int direction)
{
	if(direction == 1)
	{
		m_RotSpeed = 3.141576f;
		m_Frame1.m_pParameter = (void*)&m_RotSpeed;
		m_Frame1.SetCallback(FC_RotateUp);
		m_Frame2.m_pParameter = (void*)&m_RotSpeed;
		m_Frame2.SetCallback(FC_RotateUp);
	}
	else if(direction == 2)
	{
		m_RotSpeed = 0.0f;
		m_Frame1.m_pParameter = (void*)&m_RotSpeed;
		m_Frame1.SetCallback(FC_RotateDown);		
		m_Frame2.m_pParameter = (void*)&m_RotSpeed;
		m_Frame2.SetCallback(FC_RotateDown);
	}
	else if(direction == 3)
	{
		m_RotSpeed = 1.570788f;
		m_Frame1.m_pParameter = (void*)&m_RotSpeed;
		m_Frame1.SetCallback(FC_RotateLeft);
		m_Frame2.m_pParameter = (void*)&m_RotSpeed;
		m_Frame2.SetCallback(FC_RotateLeft);
	}
	else if(direction == 4)
	{
		m_RotSpeed = 4.712364f;
		m_Frame1.m_pParameter = (void*)&m_RotSpeed;
		m_Frame1.SetCallback(FC_RotateRight);
		m_Frame2.m_pParameter = (void*)&m_RotSpeed;
		m_Frame2.SetCallback(FC_RotateRight);
	}
}

int FC_TogglePac( CZenFrame* pFrame, void* pParameter )
{
	static bool flip = true;
	static double toggle = -0.2;

	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	pFrame->SetPitch(toggle);

	if (flip)
		toggle -= .1;
	else
		toggle += .1;

	if (toggle < -0.5) 
		flip = false;
	
	if (toggle > 0.0) 
		flip = true;
	
	return S_OK;
}
int FC_ToggleMan( CZenFrame* pFrame, void* pParameter )
{
	static bool flip = false;
	static double toggle = 0.2;

	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	pFrame->SetPitch(toggle);

	if (flip)
		toggle -= .1;
	else
		toggle += .1;

	if (toggle < 0.0) 
		flip = false;
	
	if (toggle > 0.5) 
		flip = true;
	
	return S_OK;
}
int FC_StopMovePac( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	pFrame->SetVelocity(0.0f, 0.0f, 0.0f);
	
	return S_OK;
}
int FC_RotateUp( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	// Unload the speed from the parameter
	float turn = *((float*)pParameter);
	
	// Set the rotation for this frame
	pFrame->SetYaw(turn);
	pFrame->SetVelocity(0.0f, 0.0f, 0.03f);
	
	return S_OK;
}
int FC_RotateDown( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	// Unload the speed from the parameter
	float turn = *((float*)pParameter);
	
	// Set the rotation for this frame
	pFrame->SetYaw(turn);
	pFrame->SetVelocity(0.0f, 0.0f, -0.03f);
	
	return S_OK;
}
int FC_RotateLeft( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	// Unload the speed from the parameter
	float turn = *((float*)pParameter);
	
	// Set the rotation for this frame
	pFrame->SetYaw(turn);
	pFrame->SetVelocity(-0.03f, 0.0f, 0.0f);
	
	return S_OK;
}
int FC_RotateRight( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	// Unload the speed from the parameter
	float turn = *((float*)pParameter);
	
	// Set the rotation for this frame
	pFrame->SetYaw(turn);
	pFrame->SetVelocity(0.03f, 0.0f, 0.0f);
	
	return S_OK;
}
int FC_RotateStart( CZenFrame* pFrame, void* pParameter )
{
	// Make sure the frame is valid
	if( !pFrame )
		return E_FAIL;
	
	// Set the rotation for this frame
	pFrame->SetYaw(3.141576);
	
	return S_OK;
}